package org.xxpay.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.xxpay.core.entity.ChannelAgentMchChannelInfo;
import org.xxpay.core.entity.ChannelAgentMchChannelInfoExample;
import org.xxpay.core.service.IChannelAgentMchChannelInfoService;
import org.xxpay.service.dao.mapper.ChannelAgentMchChannelInfoMapper;

import java.util.List;

/**
 * Created by xl on 2019/5/8.
 */

@Service(interfaceName = "org.xxpay.core.service.IChannelAgentMchChannelInfoService", version = "1.0.0", retries = -1)
public class ChannelAgentMchChannelInfoServiceImpl implements IChannelAgentMchChannelInfoService {
    @Autowired
    private ChannelAgentMchChannelInfoMapper  channelAgentMchChannelInfoMapper;
    @Override
    public int add(ChannelAgentMchChannelInfo channelAgentMchChannelInfo) {
        int count =  channelAgentMchChannelInfoMapper.insertSelective(channelAgentMchChannelInfo);
        return count;
    }

    @Override
    public List<ChannelAgentMchChannelInfo> select(int offset, int limit, ChannelAgentMchChannelInfo channelAgentMchChannelInfo) {
        ChannelAgentMchChannelInfoExample example = new ChannelAgentMchChannelInfoExample();
        example.setOrderByClause("createTime DESC");
        example.setOffset(offset);
        example.setLimit(limit);
        ChannelAgentMchChannelInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, channelAgentMchChannelInfo);

        return channelAgentMchChannelInfoMapper.selectByExample(example);

    }
    @Override
    public Integer count(ChannelAgentMchChannelInfo channelAgentMchChannelInfo) {
        ChannelAgentMchChannelInfoExample example = new ChannelAgentMchChannelInfoExample();
        ChannelAgentMchChannelInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, channelAgentMchChannelInfo);
        return channelAgentMchChannelInfoMapper.countByExample(example);
    }

    @Override
    public  List<ChannelAgentMchChannelInfo> selectByChannelAgentMchId(Long supChannelMchAgentId) {
        List<ChannelAgentMchChannelInfo> list =  channelAgentMchChannelInfoMapper.selectByChannelAgentMchId(supChannelMchAgentId);
        return list;
    }
    @Override
    public int update(ChannelAgentMchChannelInfo channelAgentMchChannelInfo) {
        return channelAgentMchChannelInfoMapper.updateByPrimaryKeySelective(channelAgentMchChannelInfo);
    }
    @Override
    public int batchDeleteChannelAgentMch(List<Long> channelAgentMchIds) {
        if(CollectionUtils.isEmpty(channelAgentMchIds)) return 0;
        int deleteCount = 0;
        for(Long channelAgentMchId : channelAgentMchIds) {
            deleteCount = deleteCount + channelAgentMchChannelInfoMapper.deleteByPrimaryKey(channelAgentMchId);
        }
        return deleteCount;
    }

    @Override
    public ChannelAgentMchChannelInfo find(ChannelAgentMchChannelInfo channelAgentMchChannelInfo) {
        ChannelAgentMchChannelInfoExample example = new ChannelAgentMchChannelInfoExample();
        ChannelAgentMchChannelInfoExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, channelAgentMchChannelInfo);
        List<ChannelAgentMchChannelInfo> channelAgentMchInfoList = channelAgentMchChannelInfoMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(channelAgentMchInfoList)) return null;
        return channelAgentMchInfoList.get(0);
    }

    void setCriteria(ChannelAgentMchChannelInfoExample.Criteria criteria, ChannelAgentMchChannelInfo channelAgentMchChannelInfo) {
        if (channelAgentMchChannelInfo != null) {
            if (channelAgentMchChannelInfo.getChannelAgentMchId() != null)
                criteria.andChannelAgentMchIdEqualTo(channelAgentMchChannelInfo.getChannelAgentMchId());
            //   if(channelAgentMchChannelInfo.getChannelAgentMchRate() != null) criteria.andChannelAgentMchIdEqualTo(channelAgentMchChannelInfo.getChannelAgentMchRate());
            if (channelAgentMchChannelInfo.getPaypassage() != null)
                criteria.andPaypassageEqualTo(channelAgentMchChannelInfo.getPaypassage());
            if (channelAgentMchChannelInfo.getPayproduct() != null)
                criteria.andPayproductEqualTo(channelAgentMchChannelInfo.getPayproduct());
            if (channelAgentMchChannelInfo.getStatus() != null && channelAgentMchChannelInfo.getStatus().byteValue() != -99)
                criteria.andStatusEqualTo(channelAgentMchChannelInfo.getStatus());
        }
    }
}
